package com.micookie.v2ex.ui;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.micookie.v2ex.R;
import com.micookie.v2ex.ui.base.BaseActivity;
import com.micookie.v2ex.ui.fragment.NodeListFragment;
import com.micookie.v2ex.ui.fragment.SettingsFragment;
import com.micookie.v2ex.ui.fragment.TopicListTabsFragment;

import butterknife.BindView;

public class HomeActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener {

    @BindView(R.id.toolbar_main)
    Toolbar mToolbar;

    Fragment mTopicListTabsFragment;

    Fragment mNodeListFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        setSupportActionBar(mToolbar);
    }


    @Override
    public void initData() {
        super.initData();
        showTopicListTabsFragment();
    }

    @Override
    public void initView() {
        super.initView();
        setSupportActionBar(mToolbar);
        initDrawer();
    }

    private void showTopicListTabsFragment() {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        if (mTopicListTabsFragment == null) {
            mTopicListTabsFragment = new TopicListTabsFragment();
            transaction.add(R.id.framelayout, mTopicListTabsFragment);
        }
        hideFragment();
        transaction.show(mTopicListTabsFragment);
        transaction.commit();
    }

    private void showNodeListFragment() {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        if (mNodeListFragment == null) {

            mNodeListFragment = new NodeListFragment();
            transaction.add(R.id.framelayout, mNodeListFragment);
        }
        hideFragment();
        transaction.show(mNodeListFragment);
        transaction.commit();

    }

    private void hideFragment() {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        if (mNodeListFragment != null) {
            transaction.hide(mNodeListFragment);
        }
        if (mTopicListTabsFragment != null) {
            transaction.hide(mTopicListTabsFragment);
        }
        transaction.commit();
    }


    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_explorer) {
            showTopicListTabsFragment();
        } else if (id == R.id.nav_node) {
            showNodeListFragment();
        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {
            Intent intent = new Intent(this, SettingsFragment.class);
            startActivity(intent);

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    /**
     * 初始化左侧菜单栏
     */
    private void initDrawer() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }


}
